--[[--
类名: PlayerInfo.lua
创建日期: 2015-7-24
创建人: 陈小虎
功能描述: 用户信息面板结构
修改内容：
修改日期:
修改人:
修改备注:
]]

local TeamModel = app:getInst("TeamModel")
local HeadRender = require("app.widget.renders.HeadRender")

local PlayerInfo = class("PlayerInfo")


function PlayerInfo:ctor(node)
    cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

    self._rootNode = node

    self:init()

    -- self:refreshPlayerInfo()
end

function PlayerInfo:init()
    local root = self._rootNode:getChildByName("Panel")

    -- HeadRender
    local head = root:getChildByName("head")
    local render = HeadRender.create(head, 3)
    local size = render:getPanelSize()
    local pos = render:convertToWorldSpace(cc.p(size.width *7/8, size.height/2))
    render:onClick(function(sender)
            self:dispatchEvent({name = "onSwitchOwnerPlayerMenu", pos = pos})
        end)
    self._headRender = render
    self:refreshHead()

    -- name life power
    self._nameText = root:getChildByName("name")
    self._nameTextPosx = self._nameText:getPositionX()

    self._lifeBar = root:findChild("Panel_life/LoadingBar")
    self._lifeBar:setPercent(100)
    self._powerBar = root:findChild("Panel_pow/LoadingBar")
    self._powerBar:setPercent(100)


    self._qqBtn1 = root:findChild("btn_qq_1")
    self._qqBtn2 = root:findChild("btn_qq_2")
    self._wxBtn = root:findChild("btn_wx")
    self._qqBtn1:addClickEventListener(handler(self, self._onClickQQGameWakeUp))
    self._qqBtn2:addClickEventListener(handler(self, self._onClickQQVip))
    self._wxBtn:addClickEventListener(handler(self, self._onClickWXGameWakeUp))
  

    self:refreshMsdk()
end

function PlayerInfo:getRootNode()
    return self._rootNode
end

function PlayerInfo:refreshPlayerHead()
    if not self._headRender then return end

    local params = FieldManager:getSelfBaseInfo()
    if params then
        self._headRender:setData(params)
    end
end

function PlayerInfo:refreshPlayerInfo(baseInfo)
    baseInfo = baseInfo or FieldManager:getSelfBaseInfo()
    if baseInfo then
        self._headRender:performWithDelay(function()
                -- 特意为之，否则截取头像可能是空png
                self._headRender:setData(baseInfo)
            end, 0)

        self:refreshNameLevel()
        self:refreshPower()
    end
end

function PlayerInfo:refreshNameLevel()
    self._nameText:setString(UD:getNickName() .. "  lv." .. UD:getLevel())
end

function PlayerInfo:refreshPower()
    local levelUpC = GD:queryHeroLevelUpByLevel(UD:getLevel())
    if levelUpC then
        local progress = UD:getPower() / tonumber(levelUpC.power) * 100
        self._powerBar:setPercent(progress)
    end
end

function PlayerInfo:refreshTeamFlag()
    local group = self._rootNode:getChildByName("group")
    local leaderFlag = group:getChildByName("img_leader")
    local awayFlag = group:getChildByName("img_zanli")
    leaderFlag:setVisible(false)
    awayFlag:setVisible(false)
    if TeamModel:hasTeam() then
        if TeamModel:isAwayFromTeam() then
            awayFlag:setVisible(true)
        elseif TeamModel:isLeader() then
            leaderFlag:setVisible(true)
        end
    end
end

function PlayerInfo:setLifeBar(percent)
    self._lifeBar:setPercent(percent)
end

function PlayerInfo:refreshMsdk()
    local msdkData = UD.msdkData

    local platform = app.channelAPI:getWGPlatform()
    local offsetX = self._nameTextPosx
    if platform == Enums.MSDK_PLATFORM.QQ then
        if msdkData:isMyWakeUpFromQQGame() then
            self._qqBtn1:setVisible(true)
            self._qqBtn1:setPositionX(offsetX)
            offsetX = offsetX + self._qqBtn1:getContentSize().width
        else
            self._qqBtn1:setVisible(false)
        end
        if msdkData:isVip()  then
            self._qqBtn2:setVisible(true)
            self._qqBtn2:setPositionX(offsetX)
            offsetX = offsetX + self._qqBtn2:getContentSize().width
        else
            self._qqBtn2:setVisible(false)
        end
        self._nameText:setPositionX(offsetX + 2)
    elseif platform == Enums.MSDK_PLATFORM.WX then
        self._qqBtn1:setVisible(false)
        self._qqBtn2:setVisible(false)
        if msdkData:isMyWakeUpFromWXGame() then
            self._wxBtn:setVisible(true)
            offsetX = offsetX + self._wxBtn:getContentSize().width
        end
        self._nameText:setPositionX(offsetX + 2)
    else
        self._qqBtn1:setVisible(false)
        self._qqBtn2:setVisible(false)
        self._wxBtn:setVisible(false)
        self._nameText:setPositionX(offsetX)
    end
end

function PlayerInfo:refreshHead()
    local headUrl, cachePath= UD.msdkData:getMyHeadUrlAndCachePath()
    if self._headRender and headUrl and cachePath then
        self._headRender:setAvatarIconFromUrl(headUrl, cachePath)
    end
end


function PlayerInfo:_onClickQQVip()
	
end

function PlayerInfo:_onClickQQGameWakeUp()

end

function PlayerInfo:_onClickWXGameWakeUp()

end


return PlayerInfo
